package dao;

import common.pojo.Page;
import common.spring.jdbc.MySQLHelper;
import common.utils.MyMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import pojo.ChatRecord;
import pojo.form.ChatListAddForm;
import pojo.form.ChatRecordAddForm;
import pojo.form.ChatRecordQueryForm;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by asus on 2018/11/22.
 */
@Repository
public class ChatRecordDao {
    @Autowired
    MySQLHelper mySQLHelper;

    public int addCharRecord(ChatRecordAddForm charRecordAddForm){
        return mySQLHelper.simpleInsert("chat_record", MyMapUtils.beanToMap(charRecordAddForm,false
        ));
    }



    public ChatRecord getChatRecord(Long customId,Long shopId){
        return mySQLHelper.queryForBean("select * from chat_record where customId=? and shopId=? order by id desc limit 1",ChatRecord.class,new Object[]{customId,shopId});
    }

    public Page<ChatRecord> getChatRecord(ChatRecordQueryForm queryForm, int curPage, int pageSize){
        StringBuilder sql = new StringBuilder();
        sql.append(" select a.* from chat_record a  where 1=1" );
        List list = new ArrayList();
        if(queryForm.getCustomId()!=null && queryForm.getCustomId()>0){
            sql.append(" and a.customId = ? ");
            list.add(queryForm.getCustomId());
        }
        if(queryForm.getShopId()!=null && queryForm.getShopId()>=0){
            sql.append(" and a.shopId = ? ");
            list.add(queryForm.getShopId());
        }
        sql.append(" order by a.id desc ");
        return mySQLHelper.queryForPageBean(sql.toString(),list.toArray(),ChatRecord.class,curPage,pageSize);
    }
}
